package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.util.FileUtil;
import com.sansi.pencilbook.util.PathUtil;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 功能说明: 文件上传
 *
 * @author lsy13
 */
@Slf4j
@RestController
@RequestMapping("/api/upload")
public class UploadController extends VerifyController implements BaseController{

    @PostMapping("/v/img")
    public ResponseDO uploadImg(@RequestParam("file") MultipartFile file){
        String imgPath = null;
        if (Objects.requireNonNull(file.getContentType()).contains("image")) {
            String path = PathUtil.getImagesPath();
            imgPath = path + "\\" + new Date().getTime() + "_" + file.getOriginalFilename();
            File newFile = new File(imgPath);
            try {
                file.transferTo(newFile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
                return ResponseDOUtil.setResponseDO(false, "图片上传失败");
            }
            return ResponseDOUtil.setResponseDO(true, "图片已成功上传", imgPath.substring(path.length()));
        }
        return ResponseDOUtil.setResponseDO(false, "请上传图片");
    }

    @PostMapping("/u/images")
    public ResponseDO uploadImgs(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        if (checkAdmin(request)) {
            String imgPath = null;
            if (files.length>0) {
                List<Map<String, String>> img = new ArrayList<>();
                String path = PathUtil.getImagesPath();
                for (MultipartFile file : files) {
                    if (Objects.requireNonNull(file.getContentType()).contains("image")) {
                        String name = new Date().getTime() + "_" + file.getOriginalFilename();
                        imgPath = path + "\\" + name;
                        File newFile = new File(imgPath);
                        Map<String, String> imgItem = new HashMap<>();
                        imgItem.put("img", name);
                        img.add(imgItem);
                        try {
                            file.transferTo(newFile);
                        } catch (IOException e) {
                            System.out.println(e.getMessage());
                            return ResponseDOUtil.setResponseDO(false, "图片上传失败");
                        }
                    }
                }
                return ResponseDOUtil.setResponseDO(true, "图片上传成功", img);
            }
            return ResponseDOUtil.setResponseDO(false, "请上传图片");
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登陆后再操作");
        }
    }

    @PostMapping("/u/getBookExcel")
    public void getBookExcel(@RequestParam("filename") String name, HttpServletRequest request, HttpServletResponse response){
        if (checkStore(request) || checkAdmin(request)) {
            log.info("======下载书籍列表模板======");
            String path = PathUtil.getExcelPath() + "\\" + name;
            try {
                byte[] dwnResult = FileUtil.gainDwnBytes(path);
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                // response.setContentType("application/force-download");
                response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(name, String.valueOf(StandardCharsets.UTF_8)));
                OutputStream os = response.getOutputStream();
                os.write(dwnResult);
                os.close();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }

}
